package com.sumer.psychology.photo.application.query;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sumer.psychology.common.persistence.mongodb.AbstractMongoRepository;

/**
 * 照片查询服务
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/30
 * Time: 10:09
 */
public class PhotoQueryApplicationService extends AbstractMongoRepository {
    private static final String DB_NAME = "sumer_photo";

    @Override
    protected String dbName() {
        return DB_NAME;
    }

    /**
     * 展现相册里的所有图片
     *
     * @param albumId      id 相册id
     * @param countPerPage count per page
     * @param offset       offset
     * @return json
     */
    public String listByAlbum(String albumId, int countPerPage, int offset) {
        final DBObject queryCondition = new BasicDBObject("albumId", albumId);
        DBCursor results = db().getCollection(dbName())
                .find(queryCondition)
                .skip(offset)
                .limit(countPerPage);

        return serializeWithTotalCount(queryCondition, results);
    }

    /**
     * 热门照片
     *
     * @param countPerPage count per page
     * @param offset       offset
     * @return json
     */
    public String findTopic(int countPerPage, int offset) {
        final DBObject queryCondition = new BasicDBObject("views", new BasicDBObject("$gt", 0));
        DBCursor results = db().getCollection(dbName())
                .find(queryCondition)
                .skip(offset)
                .limit(countPerPage)
                .sort(new BasicDBObject("views", -1));

        return serializeWithTotalCount(queryCondition, results);
    }

}